import {LoginModel} from '../../model/login'
import { config } from '../../config'
let login = new LoginModel()
const app = getApp()
// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile:" ",
    verifyCode:" ",
    userId:"",
    sendTime: '获取验证码',
    snsMsgWait: 60,
    cancelMobile: false,
    oneButton: [{text: '获取'}],
    cancelAuth:false
  },
  tapDialogButton(e) {
    this.setData({
        cancelMobile: false
    })
  },
  onMobileChange(e){
    this.mobile = e.detail.value
  },
  onVerifyCodeChange(e){
    this.verifyCode = e.detail.value
  },

  sendCode: function() {
    if (this.smsFlag && this.mobile && this.mobile.length === 11) {
      this.setData({
        smsFlag: false,
      });
      login.sendCode(this.mobile)
      var setInter = setInterval(() => {
        this.setData({
          smsFlag: false,
          sendTime: this.data.snsMsgWait + 's后重发',
          snsMsgWait: this.data.snsMsgWait - 1
        });
        if (this.data.snsMsgWait < 0) {
          clearInterval(setInter)
          this.setData({
            sendTime: '获取验证码',
            snsMsgWait: 60,
            smsFlag: true
          });
        }
      }, 1000);
    }
  },
  loginAction(){
    var that = this
    if (this.mobile && this.verifyCode && this.verifyCode.length === 6 && this.mobile.length === 11) {
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          login.login(that.mobile,that.verifyCode,app.globalData.userInfo.nickName,app.globalData.userInfo.avatarUrl).then(
                res=>{
                  if (res.code===0) {
                    config.isMobile = "1"
                    config.token = res.data.token
                    wx.setStorage({
                      data: res.data.userId,
                      key: 'userId',
                    })
                    wx.navigateBack({
                      complete: (res) => {
                        console.log("登录成功")
                      },
                    })
                  }
                  else{
                    wx.showToast({
                      title: res.msg,
                      icon:'none'
                    })
                  }
                },
              )
          }
        }
      })
    }else{
      wx.showToast({
        title: '请检查手机号和验证码！',
        icon: 'none',
      })
    }
  },
  getUserInfo(res){
    console.log(res)
    if(res.detail.userInfo){
    app.globalData.userInfo.nickName = res.detail.userInfo.nickName
    app.globalData.userInfo.avatarUrl = res.detail.userInfo.avatarUrl
    app.globalData.userInfo.gender = res.detail.userInfo.gender
    app.globalData.userInfo.province = res.detail.userInfo.province
    app.globalData.userInfo.city = res.detail.userInfo.city
    app.globalData.userInfo.country = res.detail.userInfo.country
    this.setData({
      cancelMobile:true,
      cancelAuth:false
    })
    }else{
      this.setData({
        cancelAuth:true
      })
    }
  },
  downLoad(){
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.smsFlag = true
    this.setData({
      smsFlag:true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    config.isLogin=false
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})